package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.config.BusinessException;
import com.emergency.data.entity.LinkmanPoint;
import com.emergency.data.entity.LinkmanPointContact;
import com.emergency.data.entity.Street;
import com.emergency.data.entity.Village;
import com.emergency.data.vo.LinkmanPointDetailsVO;
import com.emergency.data.vo.LinkmanPointHomeVO;
import com.emergency.data.vo.LinkmanPointVO;
import com.emergency.data.vo.LinkmanVO;
import com.emergency.http.DefaultResponseCode;
import com.emergency.mapper.LinkmanPointContactMapper;
import com.emergency.mapper.LinkmanPointMapper;
import com.emergency.mapper.StreetMapper;
import com.emergency.mapper.VillageMapper;
import com.emergency.service.LinkmanPointService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (LinkmanPoint)负责人联络点服务实现类
 *
 * @author makejava
 * @since 2020-09-21
 */
@Service
public class LinkmanPointServiceImpl extends ServiceImpl<LinkmanPointMapper, LinkmanPoint> implements LinkmanPointService {

    @Resource
    private LinkmanPointMapper linkmanPointMapper;

    @Resource
    private LinkmanPointContactMapper linkmanPointContactMapper;

    @Resource
    private StreetMapper streetMapper;

    @Resource
    private VillageMapper villageMapper;

    @Override
    public LinkmanPointHomeVO getLinkmanList(Integer streetId, Integer villageId, String phone, String name, Integer type) {
        //联系人查询条件
        QueryWrapper<LinkmanPointContact> linkmanQuery = new QueryWrapper<>();
        //联络点查询条件
        QueryWrapper<LinkmanPoint> pointQuery = new QueryWrapper<>();
        if (streetId != null) {
            pointQuery.eq("street_id", streetId);
        }
        if (villageId != null) {
            pointQuery.eq("village_id", villageId);
        }
        if (StrUtil.isNotEmpty(phone)) {
            linkmanQuery.like("phone", phone);
        }
        if (StrUtil.isNotEmpty(name)) {
            linkmanQuery.like("linkman", name);
        }
        //联络点列表
        List<LinkmanPointVO> pointList = linkmanPointMapper.selectList(pointQuery).stream().map(c -> {
            LinkmanPointVO vo = new LinkmanPointVO();
            //判断type
            if (type != null) {
                //村社区类型返回null
                if (type == 1 && c.getVillageId() != null) {
                    return null;
                }
                //乡镇类型返回null
                if (type == 2 && c.getVillageId() == null) {
                    return null;
                }
            }
            BeanUtil.copyProperties(c, vo);
            return vo;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        //联系人列表
        List<LinkmanVO> linkmanList = new ArrayList<>();
        //筛选后满足条件的点ID集合
        List<Long> pointIdList = pointList.stream().map(LinkmanPointVO::getId).collect(Collectors.toList());
        if (pointIdList.size() != 0) {
            linkmanQuery.in("linkman_point_id", pointIdList);
            linkmanList = linkmanPointContactMapper.selectList(linkmanQuery).stream().map(c -> {
                LinkmanVO vo = new LinkmanVO();
                BeanUtil.copyProperties(c, vo);
                LinkmanPoint point = linkmanPointMapper.selectById(c.getLinkmanPointId());
                vo.setX(point.getX());
                vo.setY(point.getY());
                vo.setLinkmanPointId(c.getLinkmanPointId());
                return vo;
            }).collect(Collectors.toList());
        }
        //返回VO
        LinkmanPointHomeVO vo = new LinkmanPointHomeVO();
        vo.setLinkmanList(linkmanList);
        vo.setPointList(pointList);
        return vo;
    }

    @Override
    public LinkmanPointDetailsVO getLinkmanPointDetail(Long id) {
        LinkmanPoint linkmanPoint = linkmanPointMapper.selectById(id);
        if (linkmanPoint == null) {
            throw new BusinessException(DefaultResponseCode.LINKMAN_POINT_NOT_EXIST);
        }
        //返回对象VO
        LinkmanPointDetailsVO detailsVO = new LinkmanPointDetailsVO();
        //联络点类型
        Integer type = linkmanPoint.getType();
        detailsVO.setType(type);
        //查询乡镇/街道
        Street street = streetMapper.selectById(linkmanPoint.getStreetId());
        BeanUtil.copyProperties(street, detailsVO);
        if (type == 1) {
            //查询村/社区
            Village village = villageMapper.selectById(linkmanPoint.getVillageId());
            BeanUtil.copyProperties(village, detailsVO);
        }
        //联络人列表
        List<LinkmanVO> linkmanList = linkmanPointContactMapper.selectList(new QueryWrapper<LinkmanPointContact>()
                .eq("linkman_point_id", id)).stream().map(c -> {
            LinkmanVO vo = new LinkmanVO();
            BeanUtil.copyProperties(c, vo);
            vo.setX(linkmanPoint.getX());
            vo.setY(linkmanPoint.getY());
            vo.setLinkmanPointId(c.getLinkmanPointId());
            return vo;
        }).collect(Collectors.toList());
        detailsVO.setLinkmanList(linkmanList);
        return detailsVO;
    }
}
